﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace YJKArch.ImportStructureProject
{
	public partial class ImportFilePathForm : System.Windows.Forms.Form
	{
		public string StructureInfoFilePath
		{
			get
			{
				return this.cmbPath.Text;
			}
		}

		public ImportFilePathForm(string pathName)
		{
			this.m_DefaultPathName = pathName;
			this.InitializeComponent();
			this.SetComboBoxDropDownWidth(this.cmbPath);
		}

		private void button1_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			if (this.m_DefaultPathName.CompareTo("") != 0)
			{
				openFileDialog.InitialDirectory = this.m_DefaultPathName;
			}
			openFileDialog.Filter = "结构导出文件|*.xml";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			string fileName = openFileDialog.FileName;
			IEnumerator enumerator = this.cmbPath.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.ToString().CompareTo(fileName) == 0)
					{
						return;
					}
				}
			}
			this.cmbPath.Items.Add(fileName);
			this.cmbPath.SelectedIndex = this.cmbPath.Items.Count - 1;
			this.SetComboBoxDropDownWidth(this.cmbPath);
		}

		private void cmbPath_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				Path.GetFileName(this.cmbPath.Text);
			}
			catch (FormatException)
			{
				MessageBox.Show("输入的文件不存在", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void ImportFilePathForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.cmbPath.Items.Count > 0)
			{
				FileStream fileStream = new FileStream(Path.GetTempPath() + "ImportFilePathForm.dat", FileMode.Create);
				StreamWriter streamWriter = new StreamWriter(fileStream);
				streamWriter.WriteLine(this.cmbPath.Items.Count);
				foreach (object obj in this.cmbPath.Items)
				{
					streamWriter.WriteLine(obj.ToString());
				}
				streamWriter.WriteLine(this.cmbPath.SelectedIndex);
				streamWriter.Flush();
				streamWriter.Close();
				fileStream.Close();
			}
		}

		private void ImportFilePathForm_Load(object sender, EventArgs e)
		{
			string path = Path.GetTempPath() + "ImportFilePathForm.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num > 0)
			{
				for (int i = 0; i < num; i++)
				{
					this.cmbPath.Items.Add(streamReader.ReadLine());
				}
			}
			this.cmbPath.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void btnClearFileList_Click(object sender, EventArgs e)
		{
			this.cmbPath.Items.Clear();
		}

		private void SetComboBoxDropDownWidth(System.Windows.Forms.ComboBox comboBox)
		{
			if (comboBox.Items.Count < 1)
			{
				comboBox.DropDownWidth = comboBox.Width;
				return;
			}
			Graphics graphics = comboBox.CreateGraphics();
			float num = 0f;
			foreach (object obj in comboBox.Items)
			{
				string text = (string)obj;
				if (num < graphics.MeasureString(text, comboBox.Font).Width)
				{
					num = graphics.MeasureString(text, comboBox.Font).Width;
				}
			}
			if (num > (float)comboBox.DropDownWidth)
			{
				comboBox.DropDownWidth = Convert.ToInt32(num);
			}
			graphics.Dispose();
		}

		private string m_DefaultPathName;
	}
}
